class FusionGatlingAttachment extends xWeaponAttachment;

var class<MinigunMuzFlash3rd>     mMuzFlashClass;
var class<MinigunMuzAltFlash3rd>  mMuzAltFlashClass;
var xEmitter            mMuzFlash3rd;

var bool                mbGotHit;
var byte OldSpawnHitCount;
var rotator             mHitRot;

var float               mCurrentRoll;
var float               mRollInc;
var float               mRollUpdateTime;

var class<xEmitter>     mShellCaseEmitterClass;
var xEmitter            mShellCaseEmitter;
var() vector            mShellEmitterOffset;


function Destroyed()
{
    if (mMuzFlash3rd != None)
        mMuzFlash3rd.Destroy();

    if (mShellCaseEmitter != None)
        mShellCaseEmitter.Destroy();

	Super.Destroyed();
}

simulated function UpdateRoll(float dt)
{
    local rotator r;

    UpdateRollTime(false);

    if (mRollInc <= 0.f)
        return;

    mCurrentRoll += dt*mRollInc;
    mCurrentRoll = mCurrentRoll % 65536.f;
    r.Roll = int(mCurrentRoll);

    SetBoneRotation('Bone_Barrel', r, 0, 1.f);
}

simulated function UpdateRollTime(bool bUpdate)
{
    local float diff;

    diff = Level.TimeSeconds - mRollUpdateTime;

    if (bUpdate)
        mRollUpdateTime = Level.TimeSeconds;

    // TODO: clean up!
    if (diff > 0.2)
    {
        mbGotHit = false;
        mRollInc = 0.f;
    }
}

/* UpdateHit
- used to update properties so hit effect can be spawn client side
*/
function UpdateHit(Actor HitActor, vector HitLocation, vector HitNormal)
{
	SpawnHitCount++;
	mHitLocation = HitLocation;
	mHitActor = HitActor;
	mHitNormal = HitNormal;
}

simulated event ThirdPersonEffects()
{
    local rotator r;
    local class<MinigunMuzFlash3rd> muzClass;

    if ( (Level.NetMode == NM_DedicatedServer) || (Instigator == None) 
		|| ((Level.TimeSeconds - LastRenderTime > 0.2) && (PlayerController(Instigator.Controller) == None)) )
        return;
	WeaponLight();

    if ( FlashCount > 0 )
	{
        if (FiringMode == 0)
        {
            muzClass = mMuzFlashClass;
            mRollInc = 65536.f*3.f;
        }
        else
        {
            muzClass = mMuzAltFlashClass;
            mRollInc = 65536.f;
        } 

        UpdateRollTime(true);
		
		mHitRot = rotator(mHitLocation - Instigator.Location);
		mbGotHit = true;
		if ( OldSpawnHitCount != SpawnHitCount )
		{
			OldSpawnHitCount = SpawnHitCount;
			GetHitInfo();
		}
        if (mMuzFlash3rd == None)
        {
            mMuzFlash3rd = Spawn(mMuzFlashClass);
            AttachToBone(mMuzFlash3rd, 'tip');
        }
        if (mMuzFlash3rd != None)
        {
            mMuzFlash3rd.Trigger(self, None);
            r.Roll = Rand(65536);
            SetBoneRotation('Bone Flash', r, 0, 1.f);
        }

        if ( (mShellCaseEmitter == None) && (Level.DetailMode != DM_Low) )
        {
            mShellCaseEmitter = Spawn(mShellCaseEmitterClass);
            if ( mShellCaseEmitter != None )
				AttachToBone(mShellCaseEmitter, 'shell');
        }
        if (mShellCaseEmitter != None)
            mShellCaseEmitter.mStartParticles++;
    }
    else
    {
        GotoState('');
    }

    Super.ThirdPersonEffects();
}

defaultproperties
{
     mMuzFlashClass=Class'XEffects.MinigunMuzFlash3rd'
     bHeavy=True
     bRapidFire=True
     LightHue=30
     LightSaturation=150
     LightBrightness=150.000000
     LightRadius=4.000000
     LightPeriod=3
     Mesh=SkeletalMesh'Weapons.RocketLauncher_3rd'
     Skins(0)=Combiner'TFMedia.Fusion.FusionGatlingCombiner1'
}
